Imports Cdm.DataCore.Custom
Imports Cdm.DataCore.Admin
Imports System.Collections.Generic
Imports Cdm.DataCore.LookupTables

Partial Class Admin_DataLoad
    Inherits System.Web.UI.Page

    Protected Sub SubmitFileButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SubmitFileButton.Click
        BindGridAndCheckFields()
    End Sub

    Private Sub BindGridAndCheckFields()
        Dim tableId As Integer = CInt(Me.FileDropDownList.SelectedValue)
        Dim requiredFieldList As IList(Of AdminRequiredField) = AdminRequiredField.ListByTableID(tableId)
        Dim specialFieldList As IList(Of AdminSpecialField) = AdminSpecialField.ListByTable(tableId, True, "FieldName")
        Dim dataLoadList As IList(Of AdminDataLoadFieldList) = AdminDataLoadFieldList.ListByTableID(tableId)
        Dim dataLoadHashTable As Hashtable = ConvertToHashTable(dataLoadList, False)
        Me.DataLoadScrollingGridView.DataSource = requiredFieldList
        DataLoadScrollingGridView.DataBind()
        CheckFields(requiredFieldList, dataLoadHashTable)

        Me.SpecialFieldsScrollingGridView.DataSource = specialFieldList
        SpecialFieldsScrollingGridView.DataBind()
        dataLoadHashTable = ConvertToHashTable(dataLoadList, True)
        CheckSpecialFields(specialFieldList, dataLoadHashTable)



    End Sub

    Private Sub SetupJavaScript()

        EntityController.JavaScriptAddChildDiv(Me.SpecialFieldsScrollingGridView.DivClientId, CInt(Me.SpecialFieldsScrollingGridView.ToolTip))
        ClientScript.RegisterStartupScript(Me.GetType(), "scrollDivs", EntityController.JavaScriptScrollingGridStartup(Me.SpecialFieldsScrollingGridView), True)

        EntityController.JavaScriptAddChildDiv(Me.DataLoadScrollingGridView.DivClientId, CInt(Me.DataLoadScrollingGridView.ToolTip))
        ClientScript.RegisterStartupScript(Me.GetType(), "scrollDivs2", EntityController.JavaScriptScrollingGridStartup(Me.DataLoadScrollingGridView), True)

        EntityController.JavaScriptSetFixedHeight(28)
    End Sub

    Private Sub ClearStatus()
        SetStatus("")
    End Sub
    Private Sub SetStatus(ByVal message As String)
        Dim mymaster As ASP.admin_admin_master = CType(Me.Master, ASP.admin_admin_master)
        mymaster.SetStatus(message)
    End Sub


    Private Function ConvertToHashTable(ByVal dl As IList(Of AdminDataLoadFieldList), ByVal isSpecial As Boolean) As Hashtable
        Dim hsh As New Hashtable
        For i As Integer = 0 To dl.Count - 1
            If dl(i).IsSpecialField = isSpecial Then
                hsh.Add(dl(i).FieldId, dl(i).FieldId)
            End If
        Next
        Return hsh
    End Function

    Private Sub CheckFields(ByVal fldList As IList(Of AdminRequiredField), ByVal hshDataLoadList As Hashtable)

        For iRow As Integer = 0 To DataLoadScrollingGridView.Rows.Count - 1
            Dim includedCheckBox As CheckBox = DataLoadScrollingGridView.Rows(iRow).FindControl("IsIncludedCheckBox")
            If fldList(iRow).IsRequired Then
                includedCheckBox.Checked = True
                includedCheckBox.Enabled = False
            ElseIf hshDataLoadList.ContainsKey(fldList(iRow).RequiredFieldId) Then
                includedCheckBox.Checked = True
                includedCheckBox.Enabled = True
            Else
                includedCheckBox.Checked = False
                includedCheckBox.Enabled = True
            End If
        Next

    End Sub

    Private Sub CheckSpecialFields(ByVal fldList As IList(Of AdminSpecialField), ByVal hshDataLoadList As Hashtable)

        For iRow As Integer = 0 To SpecialFieldsScrollingGridView.Rows.Count - 1
            Dim includedCheckBox As CheckBox = SpecialFieldsScrollingGridView.Rows(iRow).FindControl("IsIncludedCheckBox")
            If fldList(iRow).IsRequired Then
                includedCheckBox.Checked = True
                includedCheckBox.Enabled = False
            ElseIf hshDataLoadList.ContainsKey(fldList(iRow).SpecialFieldId) Then
                includedCheckBox.Checked = True
                includedCheckBox.Enabled = True
            Else
                includedCheckBox.Checked = False
                includedCheckBox.Enabled = True
            End If
        Next

    End Sub


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        SetupJavaScript()
        ClearStatus()
        If Not Me.IsPostBack Then
            BindRequiredTableList()
        End If
    End Sub

    Private Sub BindRequiredTableList()
        Dim C_EMPLOYEE_TABLE As String = "14026"
        Dim C_PHYSICIAN_TABLE As String = "14033"
        Me.FileDropDownList.DataSource = LuAdminTableList.ListRequiredFieldTables()
        Me.FileDropDownList.DataBind()
        Me.FileDropDownList.Items.Remove(FileDropDownList.Items.FindByValue(C_EMPLOYEE_TABLE))
        Me.FileDropDownList.Items.Remove(FileDropDownList.Items.FindByValue(C_PHYSICIAN_TABLE))
    End Sub

    Protected Sub SaveButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SaveButton.Click
        Dim trans As New Gentle.Framework.Transaction()
        Dim tableId As Integer = CInt(Me.FileDropDownList.SelectedValue)
        Try
            AdminDataLoadField.DeleteByTableId(tableId, trans)
            For irow As Integer = 0 To DataLoadScrollingGridView.Rows.Count - 1
                Dim includedCheckBox As CheckBox = DataLoadScrollingGridView.Rows(irow).FindControl("IsIncludedCheckBox")
                If includedCheckBox.Checked Then
                    Dim newfield As New AdminDataLoadField()
                    newfield.TableId = tableId
                    newfield.FieldId = DataLoadScrollingGridView.DataKeys(irow)(0)
                    newfield.IsSpecialField = False
                    newfield.Persist(trans)
                End If
            Next

            For irow As Integer = 0 To SpecialFieldsScrollingGridView.Rows.Count - 1
                Dim includedCheckBox As CheckBox = SpecialFieldsScrollingGridView.Rows(irow).FindControl("IsIncludedCheckBox")
                If includedCheckBox.Checked Then
                    Dim newfield As New AdminDataLoadField()
                    newfield.TableId = tableId
                    newfield.FieldId = SpecialFieldsScrollingGridView.DataKeys(irow)(0)
                    newfield.IsSpecialField = True
                    newfield.Persist(trans)
                End If
            Next
            trans.Commit()
            SetStatus("Settings saved.")
        Catch ex As Exception
            trans.Rollback()
            SetStatus("Error updating settings - " + ex.Message)
        End Try


    End Sub
End Class
